package com.demo.user;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;

import org.nature.framework.annotation.Ask;
import org.nature.framework.annotation.Ctrl;
import org.nature.framework.annotation.Inject;
import org.nature.framework.annotation.Interceptors;
import org.nature.framework.annotation.Redirect;
import org.nature.framework.annotation.Validator;
import org.nature.framework.auth.NatureAuther;
import org.nature.framework.bean.FileData;
import org.nature.framework.bean.JsonData;
import org.nature.framework.bean.Page;
import org.nature.framework.core.NatureCtrl;

import com.db.Db2;

/**
 * 用户控制层
 * 
 * 1、创建控制层继承NatureCtrl类并增加@Ctrl注解即可
 * <p>
 * 2、类@Ctrl的namespace是控制层的命名空间，所有本控制层的请求都要通过namespace
 * <p>
 * 3、属性@Inject注解是注入Service实例
 * <p>
 * 4、方法@Ask注解是对方法的映射，如果不写url则默认映射为方法名；与namespace共同组成请求路径/namespace/list
 * <p>
 * 5、返回支持直接返回页面"xxx.jsp",返回方法"xxx",返回JSON数据 JsonData,返回下载文件FileData等，绝对的简单快捷
 * <p>
 * 6、在控制层需要页面传值的只需声明public类型变量即可，无需冗长的GET/SET
 * <p>
 * 7、如需上传文件，控制层直接声明File类型变量即可，支持多文件集合上传，就像接受普通变量一样简单
 * <p>
 * 
 */
@SuppressWarnings("serial")
@Interceptors({ UserInterceptor.class })
@Ctrl(namespace = "/user")
public class UserCtrl extends NatureCtrl {
	public User user = new User();
	public File myfile;
	public ArrayList<File> files = new ArrayList<File>();
	public String action;
	public Page myPage = Page.newInstance("myPage");

	@Inject
	protected UserService userService;

	@Ask
	@Redirect
	public Object login() {
		int result = NatureAuther.login(user.getString(User.username), user.getString(User.password),user.getBoolean("rememberMe"));
		switch (result) {
		case 0:
			System.out.println("登陆成功");
			break;
		case 1:
			System.out.println("账号错误");
			break;
		case 2:
			System.out.println("密码错误");
			break;
		case 3:
			System.out.println("无权限");
			break;
		}
		return "list";
	}
	
	@Ask
	@Redirect
	public Object logout() {
		NatureAuther.logout();
		return "/"; 
	}

	@Ask(url = "/list")
	public Object list() {
		String sql1 = "select id,name,sex,age from tb_user where 1=1 order by id desc";
		page = userService.page(page,sql1);
		
		String sql="select * from tb_user order by id desc";
		myPage = userService.page(Db2.me(),myPage, sql);
		
		return "list.ftl";
	}

	@Ask
	public Object toOper() {
		if (user.getInt(User.id) != 0) {
			userService.byId(Db2.me(),user);
			action = "update";
		} else {
			action = "insert";
		}
		return "oper.ftl";
	}

	@Ask
	@Redirect
	@Validator(UserValidator.class)
	public Object insert() throws InterruptedException {
		userService.insert(Db2.me(),user, "user_cache");
		return "list";
	}

	@Ask
	@Validator(UserValidator.class)
	public Object update() throws InterruptedException {
		userService.update(Db2.me(),user, "user_cache");
		Thread.sleep(1000);
		return "list";
	}

	@Ask
	@Redirect("list")
	public Object delete() {
		userService.delete(Db2.me(),user);
		return "list";
	}

	@Ask
	public Object getJson() {
		return new JsonData(userService.byId(user));
	}

	@Ask
	public Object getFile() {
		InputStream inputStream = this.getClass().getResourceAsStream("文件.docx");
		return new FileData(inputStream, "文件.docx");
	}

}
